﻿/****************************************************
	文件：BagPanel.cs
	作者：WithoutUmbrella_Boy
	邮箱: 1026188730@qq.com
	日期：2021/02/16 16:12   	
	功能：背包面板
*****************************************************/
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

/// <summary>
/// 背包的页签状态枚举
/// </summary>
public enum E_Bag_Type
{
    Item = 1,
    Equip,
    Gem,
}


/// <summary>
/// 背包面板逻辑
/// </summary>
public class BagPanel : BasePanel
{
    public Transform content;

    private List<ItemCell> list = new List<ItemCell>();

    // Start is called before the first frame update
    void Start()
    {
        GetControl<Button>("btnClose").onClick.AddListener(() =>
        {
            UIManager.GetInstance().HidePanel("BagPanel");
        });

        // 为Toggle添加事件监听来触发数据更新
        GetControl<Toggle>("togItem").onValueChanged.AddListener(ToggleValueChange);
        GetControl<Toggle>("togEquip").onValueChanged.AddListener(ToggleValueChange);
        GetControl<Toggle>("togGem").onValueChanged.AddListener(ToggleValueChange);
    }

    public override void ShowMe()
    {
        base.ShowMe();
        ChangeType(E_Bag_Type.Item);
    }

    /// <summary>
    /// 当单选框 页签 状态改变时 处理的逻辑函数
    /// </summary>
    /// <param name="value"></param>
    private void ToggleValueChange(bool value)
    {
        if (GetControl<Toggle>("togItem").isOn)
        {
            ChangeType(E_Bag_Type.Item);
        }
        else if (GetControl<Toggle>("togEquip").isOn)
        {
            ChangeType(E_Bag_Type.Equip);
        }
        else
        {
            ChangeType(E_Bag_Type.Gem);
        }
    }

    /// <summary>
    /// 页签切换的函数
    /// </summary>
    /// <param name="type"></param>
    public void ChangeType(E_Bag_Type type)
    {
        //根据传进来的页签信息 来进行数据更新
        //默认值是道具列表信息
        List<ItemInfo> tempInfo = GameDataMgr.GetInstance().playerInfo.items;
        switch (type)
        {
            case E_Bag_Type.Equip:
                tempInfo = GameDataMgr.GetInstance().playerInfo.equips;
                break;
            case E_Bag_Type.Gem:
                tempInfo = GameDataMgr.GetInstance().playerInfo.gems;
                break;
        }

        //更新ScrollView的内容
        //先删除之前的格子
        for (int i = 0; i < list.Count; ++i)
        {
            Destroy(list[i].gameObject);
        }
        list.Clear();

        //在更新现在的数据
        //动态创建ItemCell预设体并且把它存在list里面 方便下一次更新的时候删除
        for (int i = 0; i < tempInfo.Count; ++i)
        {
            //实例化预设体并且得到身上的ItemCell脚本           
            ItemCell cell = ResMgr.GetInstance().Load<GameObject>("UI/ItemCell").GetComponent<ItemCell>();
            //Debug.Log(content.name);
            //Debug.Log(cell);
            //设置他的父对象
            //cell.transform.parent = content;
            cell.transform.SetParent(content);

            //初始化数据
            cell.InitInfo(tempInfo[i]);
            //把它存进list
            list.Add(cell);
        }
    }
}



